<?php 
if(!isset($_SESSION))
{
	session_start();
}

$_SESSION['thongbao_employee_salary']="";
include 'operations/connection.php';
include "generallibraries/funcs.php";
$selectyear = mysql_query("SELECT EXTRACT(YEAR FROM sac.time_work) as years FROM sa_check_time sac GROUP BY years");
$selectmonth = mysql_query("SELECT EXTRACT(MONTH FROM sac.time_work) as months FROM sa_check_time sac GROUP BY months");
$query_de = mysql_query("select department.IDDepartment, department_lang.DepartmentName from department, department_lang where department.IDDepartment = department_lang.IDDepartment and department_lang.LangID='".$_SESSION['lang']."'");
$mangkq=array();
while ($row=mysql_fetch_array($query_de)){
	$query11  = mysql_query("SELECT IDEmployee,FullName  FROM employee where State!=0 and IDDepartment='".$row[0]."'");
	$mangone=array();
	while($row1=mysql_fetch_array($query11)){
		$mangone[]=$row1;
	}
	$mangkq[$row[1]]=$mangone;
}
$bien = generalFunctions::xuatds($mangkq,'empl',$l_all);

	$mqd=array();
	$mc=array();
	$mem=array();
	$_SESSION['chung']=array();
	$_SESSION['quidinh']=array();
	$_SESSION['emsa']=array();
	
	$s1="SELECT is_late,is_half_dayoff,is_all_dayoff,allowance_hard,allowance_holiday,sub_salary_day 
	from sa_allowance_hard_holiday where sa_allowance_hard_holiday.is_status=1";
	$ms1=mysql_query($s1);
	if(mysql_num_rows($ms1)>0){
		while($row=mysql_fetch_array($ms1)){
			$tem=array();
			$tem[is_late]=$row[is_late];
			$tem[is_half_dayoff]=$row[is_half_dayoff];
			$tem[is_all_dayoff]=$row[is_all_dayoff];
			$tem[allowance_hard]=$row[allowance_hard];
			$tem[allowance_holiday]=$row[allowance_holiday];
			$tem[sub_salary_day]=$row[sub_salary_day];
			$mqd[]=$tem;
		}
		$_SESSION['quidinh']=$mqd;
		//print_r($_SESSION['quidinh']);
	}
	
	$s2="SELECT coefficient_insurance,number_time_work,number_max_holiday,allowance_holiday,allowance_lunch,allowance_car,allowance_hard,allowance_overtime 
	from sa_general_allowance where sa_general_allowance.is_status=1";
	$ms2=mysql_query($s2);
	if(mysql_num_rows($ms2)>0){
		while($row=mysql_fetch_array($ms2)){
			$mc[number_time_work]=$row[number_time_work];
			$mc[number_max_holiday]=$row[number_max_holiday];
			$mc[allowance_holiday]=$row[allowance_holiday];
			$mc[allowance_lunch]=$row[allowance_lunch];
			$mc[allowance_car]=$row[allowance_car];
			$mc[allowance_hard]=$row[allowance_hard];
			$mc[allowance_overtime]=$row[allowance_overtime];
			$mc[coefficient_insurance]=$row[coefficient_insurance];
		}
		$_SESSION['chung']=$mc;
		//print_r($_SESSION['chung']);
		/*	echo $_SESSION['chung'][number_time_work]."<br>";
			echo $_SESSION['chung'][number_max_holiday]."<br>";
			echo $_SESSION['chung'][allowance_holiday]."<br>";
			echo $_SESSION['chung'][allowance_lunch]."<br>";
			echo $_SESSION['chung'][allowance_car]."<br>";
			echo $_SESSION['chung'][allowance_hard]."<br>";
			echo $_SESSION['chung'][allowance_overtime]."<br>"; */
	}

	$ktview=1;
	$thang=$_REQUEST['months'];
	$nam=$_REQUEST['years'];
	$nhanvien=$_REQUEST['empl'];
	$idlog=$nam."-".$thang;
	$tinh_trang_bang_luong="";
	$nguoitao="";
	$ngaytao="";
	$nguoicn="";
	$ngaycn="";
	
	/*$ktquery="SELECT sa_employee_salary_log.idlog 
	from sa_employee_salary_log 
	where sa_employee_salary_log.idlog='".$idlog."'";*/
	
	$ktquery="SELECT sa_employee_salary_log.idlog,
	sa_employee_salary_log.date_create,
	sa_employee_salary_log.date_last_update,
	sa_employee_salary_log.is_saved,uc.FullName as uscreate, lu.FullName as uslastupdate  
	from sa_employee_salary_log left JOIN employee uc on sa_employee_salary_log.user_create=uc.IDEmployee 
	left join employee lu on sa_employee_salary_log.user_last_update=lu.IDEmployee 
	where sa_employee_salary_log.idlog='".$idlog."'";
	$mktquery=mysql_query($ktquery);
	if(mysql_num_rows($mktquery)>0){
		$ktview=0;
		while($r=mysql_fetch_array($mktquery)){
			if($r[is_saved]==0){
				$tinh_trang_bang_luong=$l_temporary_storage;
			}else{
				$tinh_trang_bang_luong=$l_confirm_and_save;
			}
			$nguoitao=$r[uscreate];
			$ngaytao=$r[date_create];
			$nguoicn=$r[uslastupdate];
			$ngaycn=$r[date_last_update];
		}
	}else{
		$tinh_trang_bang_luong=$l_chuaco;
		$nguoitao="";
		$ngaytao="";
		$nguoicn="";
		$ngaycn="";
	}
	
if(isset($_REQUEST['btview'])){
	if($ktview==0){
		$query="SELECT sa_employee_salary.*, employee.FullName, employee.Position,employee.IDEmployee  
		from sa_employee_salary, employee 
		where employee.IDEmployee=sa_employee_salary.employeeID ";
		if($thang !="" && $nam!=""){
			$query .=" and sa_employee_salary.sa_time='".$idlog."'";
		}
		if($nhanvien!="ALL"){
			$query .=" and  employee.IDEmployee='".$nhanvien."'";
		}
		$query .=" and employee.State=1 order by  employee.IDEmployee asc";
		
		$result=mysql_query($query);
		if(mysql_num_rows($result)>0){
			while($row=mysql_fetch_array($result)){
				$t=array();
				$t[employeeID]=$row[IDEmployee];
				$t[name]=$row[FullName];
				$t[position]=$row[Position];
				$t[number_time_work]=$row[number_time_work];
				$t[really_number_time_work]=$row[really_number_time_work];
				$t[number_allow_holiday]=$row[number_allow_holiday];
				$t[basic_salary]=$row[basic_salary];
				$t[allowance_basic_salary]=$row[allowance_basic_salary];
				
				$t[number_dateoff]=$row[number_dateoff];
				$t[number_holiday]=$row[number_holiday];
				
				$t[number_is_comeback_early]=$row[number_is_comeback_early];
				
				$t[number_is_late]=$row[number_is_late];
				$t[allowance_senior]=$row[allowance_senior];
				$t[allowance_c]=$row[allowance_c];
				
				$t[oneday_salary]=$row[oneday_salary];
				
				$t[paid_salary]=$row[paid_salary];
				
				$t[allowance_holiday]=$row[allowance_holiday];
				$t[allowance_lunch]=$row[allowance_lunch];
				$t[allowance_car]=$row[allowance_car];
				
				$t[allowance_hard]=$row[allowance_hard];
				
				
				$t[number_overtime]=$row[number_overtime];
				$t[overtime_salary]=$row[overtime_salary];
				
				$t[total_salary]=$row[total_salary];
				
				$t[insurance]=$row[insurance];
				$t[salary_bornus]=$row[salary_bornus];
				$t[salary_sub]=$row[salary_sub];
				
				$t[received_salary]=$row[received_salary];
				
				$t[note]=$row[note];
				$t[is_saved]=$row[is_saved];
				$t[is_sendmail]=$row[is_sendmail];
				
				$mem[$row[IDEmployee]]=$t;
			}
			//print_r($mem);
		}
	}else{
		
	}
}
$_SESSION['emsa']=$mem;
?>
<script type="text/javascript">

function sendmail(nam,thang){
	var listid="";
	$("input[id='chon']").each(function(){
		if (this.checked) listid = listid+","+this.value;
    	})
	listid=listid.substr(1);	 
	if (listid=="") { alert("<?php echo $l_nochoose;?>"); return;}
	hoi= confirm("<?php echo $l_areyousure;?>");
	if (hoi==true) {
		if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	 		xmlhttp=new XMLHttpRequest();
	 	}else{// code for IE6, IE5
	 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	 	}
	 	xmlhttp.onreadystatechange=function(){
			 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
			 		document.getElementById("showtb").innerHTML=xmlhttp.responseText;
			 		var tb=document.getElementById("viewthongbao").value;
			 		if(tb!=""){
			 			alert(tb);
			 		}else{
						alert("null");
			 		}
			 	}
		 	}
	 	xmlhttp.open("POST","ajax/salary/sa_sendmail.php?thang="+thang+"&nam="+nam+"&listid="+listid,true);
	 	xmlhttp.send();
	}
}
</script>
<link	href="css/style.css" rel="stylesheet" type="text/css" />
<form id="sa_f_employee_salary" name="nsa_f_employee_salary" method="post" action="">
<div id="contains">
	<div class="title" style="padding-top: 5px;font-weight: bold;"><?php echo $l_menu_privateinfo;?> &rarr; <?php echo $l_quanly_luong;?></div>
	<div class="content_code">
		<div class="approval-right">
			<div style="width:820px;border: 1px solid silver; padding: 10px 0; margin-bottom: 30px; background:white">
				<table width="820px" style="padding:0 10px">
					<tr>
						<td><b><?php echo $l_month?></b> </td>
						<td><b><?php echo $l_year?></b> </td>
						<td><b><?php echo $l_employee?></b> </td>
						<td></td>
					</tr>
					<tr>
						<td width="80px">
							<select style="width:50px;height:24px;font-size:12px;" id="months" name="months">
								<?php while ($rowmonth = mysql_fetch_array($selectmonth)){?>
									<option value="<?=$rowmonth[months]?>"  
										<?php 
											if($_REQUEST['months']==$rowmonth[months]){
												?>
												selected="selected"
												<?php 
											}
										?>
									>
										<?php echo $rowmonth[months]?>
									</option>
								<?php }?>
							</select> 
						</td>
						<td width="130px">
							<select style="width:100px;height:24px;font-size:12px;" id="years" name="years">
								<?php while ($rowyear = mysql_fetch_array($selectyear)){?>
									<option value="<?=$rowyear[years]?>" 
										<?php 
											if($_REQUEST['years']==$rowyear[years]){
												?>
												selected="selected"
												<?php 
											}
										?>
									>
										<?php echo $rowyear[years]?>
									</option>
								<?php }?>
							</select>
						</td>
						<td width="300px">
							<?php echo $bien; ?>
						</td>
						<td align="left">
							<input type="submit" name="btview" value="<?php echo $l_View?>"  class="btnupbg btnupload" />
						</td>
					</tr>
				</table>
			</div>
			<div  style="width:100%;border: 1px solid silver; padding: 10px 0; margin-bottom: 30px;">
				<table style="width:820px;">
					<tr>
						<td><b><?php echo $l_salary_satus;?></b></td>
					</tr>
					<tr>
						<td>
							<table style="padding:10px 5px" width="100%; background:white;">
								<tr>
									<td class="tdfirst"><?php echo $l_time;?></td>
									<td colspan="3" class="in_testfield" style="background:white;" ><?php echo $thang."-".$nam;?></td>
								</tr>
								<tr>
									<td class="tdfirst"><?php echo $l_salary_satus;?></td>
									<td colspan="3" class="in_testfield" style="background:white;"  ><?php echo $tinh_trang_bang_luong;?></td>
								</tr>
								<tr>
									<td class="tdfirst"><?php echo $l_create_by;?></td>
									<td class="in_testfield"  style="background:white;" ><?php echo $nguoitao;?></td>
									<td class="tdfirst"><?php echo $l_first_create;?></td>
									<td class="in_testfield" style="background:white;"  ><?php echo $ngaytao;?></td>
								</tr>
								<tr>
									<td class="tdfirst"><?php echo $l_user_last_update;?></td>
									<td class="in_testfield"  style="background:white;" ><?php echo $nguoicn;?></td>
									<td class="tdfirst"><?php echo $l_time_last_update;?></td>
									<td class="in_testfield" style="background:white;"  ><?php echo $ngaycn;?></td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			
			<div style="max-width: 100%">
				<table class="rf-dt" width="100%">
					<tr>
						<!--<th rowspan="2" class="rf-dt-shdr-c"><input type="checkbox" name="chonhetroi" id="chonhetroi" onclick="clickChonHetRoi(this);"/></th>  -->
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_no;?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_employee_name?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_position?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_number_time_work?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_num_allow_holiday?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_basicsalary?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowancebasicsalary?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_dayoff?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_absentmonth?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_comebackearly?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo  $l_comelately?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowanceSenior?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowanceC?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_salaryperday?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_salaryreceived?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowance_holiday?></th>
						<th colspan="2" class="rf-dt-shdr-c"><?php echo $l_allowance?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowance_hard?></th>
						<th colspan="2" class="rf-dt-shdr-c"><?php echo $l_allowance_overtime?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_total?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_socialInsurance?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_salary_bonus?></th>
						<!--<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_salary_sub?></th>  -->
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_realreceived?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_note;?></th>
					</tr>
					<tr>
						<th class="rf-dt-shdr-c"><?php echo $l_lunch?></th>
						<th class="rf-dt-shdr-c"><?php echo $l_parking?></th>
						<th class="rf-dt-shdr-c"><?php echo $l_numbersession?></th>
						<th class="rf-dt-shdr-c"><?php echo $l_total_amount?></th>
					</tr>
					<tr style="background: #93cddd; color: white;text-align:center;">
						<!-- <td class="rf-dt-c"></td> -->
						<td class="rf-dt-c"></td>
						<td class="rf-dt-c">(2)</td>
						<td class="rf-dt-c">(3)</td>
						<td class="rf-dt-c">(4)</td>
						<td class="rf-dt-c"></td>
						<td class="rf-dt-c">(5)</td>
						<td class="rf-dt-c">(6)</td>
						<td class="rf-dt-c">(7)</td>
						<td class="rf-dt-c">(8)</td>
						<td class="rf-dt-c">(9)</td>
						<td class="rf-dt-c">(10)</td>
						<td class="rf-dt-c">(11)</td>
						<td class="rf-dt-c">(12)</td>
						<td class="rf-dt-c">(13)=[(5)+(6) +(11)+(12)]/4</td>
						<td class="rf-dt-c">(14)=(13) x [(4)-7)]</td>
						<td class="rf-dt-c">(15)</td>
						<td class="rf-dt-c">(16)</td>
						<td class="rf-dt-c">(17)</td>
						<td class="rf-dt-c">(18)</td>
						<td class="rf-dt-c">(19)</td>
						<td class="rf-dt-c">(20)= 240000x(19)</td>
						<td class="rf-dt-c">(21)=(14)+(15) +(16)+(17) +(28)+(20)</td>
						<td class="rf-dt-c">(22)</td>
						<td class="rf-dt-c">(23)</td>
						<!-- <td class="rf-dt-c">(24)</td> -->
						<td class="rf-dt-c">(24)=(21)+(23)-(22)</td>
						<td class="rf-dt-c"></td>
					</tr>
					
					<?php 
						if(count($mem)>0){
							$vitri=0;
							$sum_total=0;
							foreach($mem as $khoa=>$giatri){
								?>
								<tr>
									<!-- <td class="rf-dt-c"><input type="checkbox" id="chon" name="xoa[]" value="<?=$giatri[employeeID]?>" /></td> -->
									<td class="rf-dt-c"><?php echo ($vitri+1);?></td>
									<td class="rf-dt-c"><?php echo $giatri[name];?></td>
									<td class="rf-dt-c"><?php echo $giatri[position];?></td>
									<td class="rf-dt-c"><?php echo $giatri[really_number_time_work]."/".$giatri[number_time_work];?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_allow_holiday];?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[basic_salary]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_basic_salary]);?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_dateoff];?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_holiday];?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_is_comeback_early];?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_is_late];?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_senior]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_c]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[oneday_salary]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[paid_salary]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_holiday]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_lunch]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_car]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_hard]);?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_overtime];?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[overtime_salary]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[total_salary]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[insurance]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[salary_bornus]);?></td>
									<!-- <td class="rf-dt-c"><?php echo number_format($giatri[salary_sub]);?></td> -->
									<td class="rf-dt-c"><?php echo number_format($giatri[received_salary]);?></td>
									<td class="rf-dt-c"><?php echo $giatri[note];?></td>
								</tr>
								<?php 
								$sum_total +=$giatri[received_salary];
								$vitri++;
							}
						}
					?>
					<tr>
						<td colspan="24" class="rf-dt-c"></td>
						<td class="rf-dt-c" style="background: #93cddd;font-weight: bold;"><?php echo number_format($sum_total);?></td>
						<td class="rf-dt-c"></td>
					</tr>
				</table> 
			</div>
			<div style="width:100%;align:center;" id="showtb">
				<input type="hidden" name="viewthongbao" id="viewthongbao" value="<?php echo $_SESSION['thongbao_employee_salary'];?>" />
			</div>
			<div style="margin-top: 15px; text-align: center; padding-right: 10px">
			<?php 
				/*if(count($mem)>0 && $ktview==0){
					?>
					<!-- <input type="submit" name="btexportexcel" id="btexportexcel" value="<?php echo $l_export_excel?>"  class="btnupbg btnupload" /> -->
					<!-- <input type="button" name="btsendmail" onclick="sendmail(<?=$_REQUEST[years]?>,<?=$_REQUEST[months]?>);" id="btsendmail"  value="<?php echo $l_sendmail?>"  class="btnupbg btnupload" /> -->
					<?php
				}*/
			?>
				
			</div>
			</div>
		</div>
	</div>
</div>
</form>